package engineer.clientapp.tools;

import java.sql.SQLException;
import java.util.Random;

import org.apache.commons.codec.digest.DigestUtils;


public class Tool {
	public static boolean remindPassword(String email) {
		Connector con;
		try {
			   con=new Connector();
			   String alphabet = "123456789abcdefghijklmnoprstuwyz";
			   Random r = new Random();
			   String helpPassword = ""; 
			   for (int i = 0; i < 10; i++) {
				   helpPassword += alphabet.charAt(r.nextInt(alphabet.length()));
			   }
			   
			con.getStatement().executeUpdate("UPDATE automats.users SET `password` = \""+DigestUtils.md5Hex(helpPassword)+"\" where email = \""+email+"\"");
			new SendEmail().sendPasswordEmail(email, helpPassword);
			con.closeConnection();
			return true;
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (Exception exception) {
			exception.printStackTrace();
		}
		return false;
	}
	
	public static boolean checkEmailFormat(String email){
		 String emailRegex= "^[\\w-_\\.+]*[\\w-_\\.]\\@([\\w]+\\.)+[\\w]+[\\w]$";
		 boolean check=email.matches(emailRegex);
		 if(check){
			 return true;
		 }else{
			 return false;
		 }
	}
}
